//
//  TYZDrawView.m
//  LiaoShenJudicial
//
//  Created by tangbin on 2020/6/22.
//  Copyright © 2020 TangBin. All rights reserved.
//

#import "TYZDrawView.h"
#import "TYZDrawBezierPath.h"

@interface TYZDrawView ()

/// 用来管理画板上所有的路径
@property (nonatomic, strong) NSMutableArray *paths;
@end

@implementation TYZDrawView

#pragma mark - private tmhods

/// 保存图片回调
/// @param image 图片
/// @param error error description
/// @param contextInfo contextInfo description
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *message = @"";
    if (!error)
    {
        message = @"成功保存到相册";
    }
    else
    {
        message = [error description];
    }
    NSLog(@"message is %@",message);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 获取触摸对象
    UITouch *touch = [touches anyObject];
    
    // 获取手指的位置
    CGPoint point = [touch locationInView:touch.view];
    
    // 当手指按下的时候就创建一条路径
    TYZDrawBezierPath *path = [TYZDrawBezierPath bezierPath];
    
    // 设置画笔宽度
    if (_lineWidth <= 0)
    {
        [path setLineWidth:5];
    }
    else
    {
        [path setLineWidth:_lineWidth];
    }
    
    // 设置画笔颜色
    [path setLineColor:_lineColor];
    
    // 设置起点
    [path moveToPoint:point];
    
    // 把每一次新创建的路径 添加都数组当中
    [self.paths addObject:path];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 获取触摸对象
    UITouch *touch = [touches anyObject];
    // 获取手指的位置
    CGPoint point = [touch locationInView:touch.view];
    // 连线的点
    [[self.paths lastObject] addLineToPoint:point];
    
    // 重绘
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    
    for (TYZDrawBezierPath *path in self.paths)
    {
        if ([path isKindOfClass:[UIImage class]]) {
            // 画图片
            UIImage* image = (UIImage*)path;
            [image drawInRect:rect];
        }else{
            // 设置颜色
            [path.lineColor set];
            
            // 设置连接处的样式
            [path setLineJoinStyle:kCGLineJoinRound];
            
            // 设置头尾的样式
            [path setLineCapStyle:kCGLineCapRound];
            
            // 渲染
            [path stroke];
        }
        
    }
}

#pragma mark - setter getter

- (NSMutableArray *)paths
{
    if (!_paths)
    {
        _paths = [NSMutableArray array];
    }
    return _paths;
}

#pragma - public methds
/// 清除画板
- (void)clean
{
    [self.paths removeAllObjects];
    // 重绘
    [self setNeedsDisplay];
}

/// 回退上一步
- (void)undo
{
    [self.paths removeLastObject];
    [self setNeedsDisplay];
}

/// 橡皮擦
- (void)eraser
{
    _lineColor = self.backgroundColor;
}

/// 保存到相册
- (UIImage *)save:(BOOL)isSave
{
    if (self.paths.count == 0)
    {
        return nil;
    }
    //开启图片上下文
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
    //获取上下文
    CGContextRef context=UIGraphicsGetCurrentContext();
    //截屏
    [self.layer renderInContext:context];
    //获取图片
    UIImage *image= UIGraphicsGetImageFromCurrentImageContext();
    //关闭图片上下文
    UIGraphicsEndImageContext();
    if (isSave)
    {
        //保存到相册
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
    }
    
    
    return image;
}

// 重写image属性
- (void)setImage:(UIImage *)image{

    _image = image;
    
    // 将图片加入到线条数组中
    [self.paths addObject:image];
    
    [self setNeedsDisplay];
}
@end

